package com.att.service.impl;

import com.att.bean.AccessLogBean;
import com.att.dao.AccessLogDaoMapper;
import com.att.service.AccessLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("accessLogService")
public class AccessLogServiceImpl implements AccessLogService {

    @Resource
    private AccessLogDaoMapper accessLogDaoMapper;

    @Override
    public List<AccessLogBean> findByCondition(AccessLogBean bean) {
        return accessLogDaoMapper.findByCondition(bean);
    }

    @Override
    public List<AccessLogBean> findByConditionCount(AccessLogBean bean) {
        return accessLogDaoMapper.findByConditionCount(bean);
    }

    @Override
    public List<AccessLogBean> findStationByCondition(AccessLogBean bean) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<AccessLogBean> arrayList = new ArrayList<>();
        //当天时间为0点的Date
        if (bean.getCheckTime() != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(bean.getCheckTime());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date startTime = calendar.getTime();
            //开始时间
            bean.setStartTime(startTime);
            calendar.add(Calendar.DAY_OF_MONTH, +1);
            Date alterTime = calendar.getTime();
            bean.setEndTime(alterTime);
        }
        List<AccessLogBean> list = accessLogDaoMapper.findStationByCondition(bean);
        for (int i = 0; i < list.size(); i++) {
            if (bean.getStationType() != null) {
                if (bean.getStationType() == 1) {
                    if (list.get(i).getEventTime() == null || list.get(i).getEventTime() == "") {
                        if(list.get(i).getCheckTime() == null){
                            list.get(i).setIsType(1);
                            arrayList.add(list.get(i));
                        }
                    }
                }
                if (bean.getStationType() == 0) {
                    if (list.get(i).getEventTime() != null) {
                        list.get(i).setIsType(0);
                        arrayList.add(list.get(i));
                    }
                    if(list.get(i).getCheckTime() != null){
                        list.get(i).setIsType(0);
                        list.get(i).setEventTime(format.format(list.get(i).getCheckTime()));
                        arrayList.add(list.get(i));
                    }
                }
            } else {
                if (list.get(i).getEventTime() == null || list.get(i).getEventTime() == "") {
                    if(list.get(i).getCheckTime() == null) {
                        list.get(i).setIsType(1);
                        arrayList.add(list.get(i));
                    }else {
                        list.get(i).setIsType(0);
                        list.get(i).setEventTime(format.format(list.get(i).getCheckTime()));
                        arrayList.add(list.get(i));
                    }
                } else {
                    list.get(i).setIsType(0);
                    arrayList.add(list.get(i));
                }
            }
        }
        return arrayList;
    }

    @Override
    public List<AccessLogBean> findStationByConCount(AccessLogBean bean) {
        List<AccessLogBean> arrayList = new ArrayList<>();
        //当天时间为0点的Date
        if (bean.getCheckTime() != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(bean.getCheckTime());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date startTime = calendar.getTime();
            //开始时间
            bean.setStartTime(startTime);
            calendar.add(Calendar.DAY_OF_MONTH, +1);
            Date alterTime = calendar.getTime();
            bean.setEndTime(alterTime);
        }
        List<AccessLogBean> count = accessLogDaoMapper.findStationByConCount(bean);
        for (int i = 0; i < count.size(); i++) {
            if (bean.getStationType() != null) {
                if (bean.getStationType() == 1) {
                    if (count.get(i).getEventTime() == null || count.get(i).getEventTime() == "") {
                        count.get(i).setIsType(1);
                        arrayList.add(count.get(i));
                    }
                }
                if (bean.getStationType() == 0) {
                    if (count.get(i).getEventTime() != null) {
                        count.get(i).setIsType(0);
                        arrayList.add(count.get(i));
                    }
                }
            } else {
                if (count.get(i).getEventTime() == null || count.get(i).getEventTime() == "") {
                    count.get(i).setIsType(1);
                    arrayList.add(count.get(i));
                } else {
                    count.get(i).setIsType(0);
                    arrayList.add(count.get(i));
                }
            }
        }
        return arrayList;
    }

    @Override
    public List<AccessLogBean> findAccessLogErrorCondition(AccessLogBean bean) {
        return accessLogDaoMapper.findAccessLogErrorCondition(bean);
    }

    @Override
    public List<AccessLogBean> findAccessLogErrorCount(AccessLogBean bean) {
        return accessLogDaoMapper.findAccessLogErrorCount(bean);
    }

    @Override
    public List<AccessLogBean> findAccessLogBean(AccessLogBean bean) {
        return accessLogDaoMapper.findAccessLogBean(bean);
    }

    @Override
    public List<AccessLogBean> findAccessLogCountCondition(AccessLogBean bean) {
        return accessLogDaoMapper.findAccessLogCountCondition(bean);
    }

    @Override
    public List<AccessLogBean> findAccessLogConCount(AccessLogBean bean) {
        return accessLogDaoMapper.findAccessLogConCount(bean);
    }

    @Override
    public List<AccessLogBean> findCheckInOutBean(AccessLogBean bean) {
        return accessLogDaoMapper.findCheckInOutBean(bean);
    }

    @Override
    public List<AccessLogBean> findCheckInOutBeanCount(AccessLogBean bean) {
        return accessLogDaoMapper.findCheckInOutBeanCount(bean);
    }

    @Override
    public boolean delete(Integer id) {
        return accessLogDaoMapper.delete(id);
    }
}
